package com.future.lock.find;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.lock.R;
import com.future.lock.common.Constants;
import com.future.lock.common.MyApp;
import com.future.lock.me.activity.LoginActivity;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

public class NewsDetailActivity extends BaseActivity {

    private static final String TAG = "NewsDetailActivity";

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.webView)
    WebView webView;
//    @BindView(R.id.videoCon)
//    ViewGroup viewGroup;

    private String id;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String url = webView.getUrl();
                if (!url.contains("user_token") && !TextUtils.isEmpty(MyApp.getUserToken())) {
                    url += "/user_token/" + MyApp.getUserToken();
                    webView.loadUrl(url);
                }else{
                    webView.reload();
                }
            }
        }
    };
    private String url;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_news_detail);
        ButterKnife.bind(this);
    }

    @SuppressLint("JavascriptInterface")
    @Override
    protected void initView() {
        setTitle("资讯详情");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.addJavascriptInterface(this, "localMethod");
        url = Constants.NEWS_URL + id;
        if (!TextUtils.isEmpty(MyApp.getUserToken())) {
            url += "/user_token/" + MyApp.getUserToken();
        }
        webView.loadUrl(url);
    }

    @JavascriptInterface
    public String getUserToken() {
        return MyApp.getUserToken();
    }

    @JavascriptInterface
    public void toLogin() {
        startActivity(LoginActivity.class);
    }

    @JavascriptInterface
    public void showShare(String title, String content, String url,String imgUrl) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                if ("Wechat".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                }
                if ("WechatMoments".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                }
                if ("QQ".equals(platform.getName())) {
                    paramsToShare.setTitle(title);
                    paramsToShare.setTitleUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                }
                if("QZone".equals(platform.getName())){
                    paramsToShare.setTitle(title);
                    paramsToShare.setTitleUrl(url);
                    paramsToShare.setText(content);
                    paramsToShare.setImageUrl(imgUrl);
                }
            }
        });

        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                toast("分享成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                toast("分享失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                toast("分享取消");
            }
        });

        // 启动分享GUI
        oks.show(this);
    }

    @Override
    protected void initLogic() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        id = extras.getString("id");
    }

    @Override
    protected void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView.onResume();
        handler.sendEmptyMessage(1);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @JavascriptInterface
    public void reload() {
        handler.sendEmptyMessage(1);
    }


}
